package com.bsoft.gol.hcb.dao.prescriptionCirculation;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import pcn.bean.consultation.ExpressOrderDetailVo;
import pcn.bean.consultation.ExpressOrderQo;
import pcn.bean.consultation.ExpressOrderVo;
import pcn.bean.express.QueryDrugOrderExpressInfoListVo;
import pcn.consultation.ExpressOrder;

import java.util.ArrayList;
import java.util.List;

public abstract class ExpreeOrderDAO extends HibernateSupportDelegateDAO<ExpressOrder> {
    public ExpreeOrderDAO(){
        this.setKeyField("expressId");
    }

    public List<ExpressOrderVo> findExpreeOrder(final ExpressOrderQo expressOrderQo){
        HibernateStatelessResultAction<List<ExpressOrderVo>> action = new AbstractHibernateStatelessResultAction<List<ExpressOrderVo>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("select i.content content,t.level as \"level\" from ");
                Query query = ss.createSQLQuery(hsql.toString());
                query.setParameter("recordId", expressOrderQo);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List listi = query.list();
                if (listi.size() == 0) {
                    setResult(null);
                } else {

                    List<ExpressOrderVo> list = new ArrayList<>();
                    for (Object o : listi) {
                        list.add(ConversionUtils.convert(o, ExpressOrderVo.class));
                    }

                    setResult(list);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @DAOMethod(sql = "from ExpressOrder where orderId =:orderId and deleteFlag='0'")
    public abstract ExpressOrder getByOrderId(@DAOParam("orderId")String orderId);

    @DAOMethod(sql = "from ExpressOrder where orderId =:orderId and deleteFlag='0'")
    public abstract List<ExpressOrder> findByOrderId(@DAOParam("orderId")String orderId);

    public List<ExpressOrder> findByOrderIds(final List<String> orderIds){
        HibernateStatelessResultAction<List<ExpressOrder>> action = new AbstractHibernateStatelessResultAction<List<ExpressOrder>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer("select eo.express_code as expressCode,eod.express_order_id as expressId from express_order eo left join express_order_detail eod on eod.express_id = eo.express_id where order_id in(:orderIds) and eo.delete_flag='0';");
                Query query = ss.createSQLQuery(hsql.toString());
                query.setParameterList("orderIds",orderIds);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List listi = query.list();
                if (listi.size() == 0) {
                    setResult(null);
                } else {

                    List<ExpressOrder> list = new ArrayList<>();
                    for (Object o : listi) {
                        list.add(ConversionUtils.convert(o, ExpressOrder.class));
                    }

                    setResult(list);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<ExpressOrderDetailVo> getExpressOrder(final String orderId ){
        HibernateStatelessResultAction<List<ExpressOrderDetailVo>> action = new AbstractHibernateStatelessResultAction<List<ExpressOrderDetailVo>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("select express_name as expressName,express_order_id as expressOrderId from express_order_detail eo where eo.express_id in (select express_id from express_order where order_id=:orderId)" );
                Query query = ss.createSQLQuery(hsql.toString());
                query.setParameter("orderId",orderId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List listi = query.list();
                if (listi.size() == 0) {
                    setResult(null);
                } else {

                    List<ExpressOrderDetailVo> list = new ArrayList<>();
                    for (Object o : listi) {
                        list.add(ConversionUtils.convert(o, ExpressOrderDetailVo.class));
                    }

                    setResult(list);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<QueryDrugOrderExpressInfoListVo> queryExpressOrderDetailList(final String orderId ){
        HibernateStatelessResultAction<List<QueryDrugOrderExpressInfoListVo>> action = new AbstractHibernateStatelessResultAction<List<QueryDrugOrderExpressInfoListVo>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("select eo.express_fee as expressFee,eo.express_code as expressCode,eo.detail_id as expressId,eo.express_order_id as expressNumber,eo.deliver_flag as expressStatus from express_order_detail eo where eo.express_id in (select express_id from express_order where order_id=:orderId)" );
                Query query = ss.createSQLQuery(hsql.toString());
                query.setParameter("orderId",orderId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List listi = query.list();
                if (listi.size() == 0) {
                    setResult(null);
                } else {

                    List<QueryDrugOrderExpressInfoListVo> list = new ArrayList<>();
                    for (Object o : listi) {
                        list.add(ConversionUtils.convert(o, QueryDrugOrderExpressInfoListVo.class));
                    }

                    setResult(list);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}
